<?php
/**
 * @author 	 	 supfire@sohu.com
 * @copyright		 yctool.com
 * @version		 1.0
 * @name 		 roles.action.php
 * @todo 		 权限设置程序处理
 * @final 		 2009-05-09
 */
!defined("IN_YCTOOL") && exit();

//获取POST/GET方法提交的变量
$actionPost				= isset($_POST['actionPost']) ? $_POST['actionPost'] : "";
$username				= isset($_POST['username']) ? $_POST['username'] : "";
$userid					= isset($_POST['userid']) ? $_POST['userid'] : "";
!$userid && $userid			= isset($_GET['userid']) ? $_GET['userid'] : "";
$groupid				= isset($_GET['groupid']) ? $_GET['groupid'] : "";
$cellid					= isset($_POST['cellid']) ? $_POST['cellid'] : "";
!$cellid && $cellid			= isset($_GET['cellid']) ? $_GET['cellid'] : "";

$cellname				= isset($_POST['cellname']) ? $_POST['cellname'] : "";
$celltitle				= isset($_POST['celltitle']) ? $_POST['celltitle'] : "";
$celltype				= isset($_POST['celltype']) ? $_POST['celltype'] : 0;
$cellallow				= isset($_POST['cellallow']) ? $_POST['cellallow'] : 0;
$parentid				= isset($_POST['parentid']) ? $_POST['parentid'] : 0;
$cellorder				= isset($_POST['cellorder']) ? $_POST['cellorder'] : 0;

$groupcells				= isset($_POST['groupcells']) ? $_POST['groupcells'] : 0;
$groupname				= isset($_POST['groupname']) ? $_POST['groupname'] : '';
$grouptype				= isset($_POST['grouptype']) ? $_POST['grouptype'] : 0;
$grouporder				= isset($_POST['grouporder']) ? $_POST['grouporder'] : 0;

$operation				= isset($_GET['operation']) ? $_GET['operation'] : "";

//$theurl = 'admin.php?action=roles&operation='.$operation;
$theurl = 'admin.php?action=roles';

//操作数据库
//添加修改权限
if ($actionPost == 'cell')
{
    (!$cellname || strlen($cellname) > 254) && func::showMsg("长度要求在200字符以下，请重新输入！", '../');
    //操作数据库
    //得到要执行的SQL中的字段
    if ($_POST['cellid'])
    {
	$pid = db::getOne(DB_TABLEPRE . 'user_authcells', 'cellid', "cellid = '$cellid'");
	if (!$pid)
	{
		 func::showMsg('ID为“'.$cellid.'” 不存在！', '../');
	}
	//更新语句
	$sql = 'UPDATE '.DB_TABLEPRE . 'user_authcells SET';
	$sql .= " cellname = '$cellname',";
	$sql .= " celltitle = '$celltitle',";
	$sql .= " parentid = '$parentid',";
	$sql .= " cellorder = '$cellorder',";
	$sql .= " cellallow = '$cellallow',";
	$sql .= " celltype = $celltype";
	$sql .= " WHERE cellid = $cellid ";
    }
    else
    {
	$pid = db::getOne(DB_TABLEPRE . 'user_authcells', 'cellid', "cellname = '$cellname'");
	if ($pid)
	{
		 func::showMsg('“'.$cellname.'” 已经存在！', '../');
	}
	//插入语句
	$sql = 'INSERT INTO '.DB_TABLEPRE . 'user_authcells(';
	$sql .= " cellname,";
	$sql .= " celltitle,";
	$sql .= " parentid,";
	$sql .= " cellorder,";
	$sql .= " cellallow,";
	$sql .= "  celltype ";
	$sql .= ") VALUES(";
	$sql .= " '$cellname',";
	$sql .= " '$celltitle',";
	$sql .= " '$parentid',";
	$sql .= " '$cellorder',";
	$sql .= " '$cellallow',";
	$sql .= " $celltype";
	$sql .= ")";

    }
    //插入或者更新资料
    $isok = db::Query($sql);
    if($isok)
    {
	//写入缓存
	$authlist = db::getLimit(DB_TABLEPRE . 'user_authcells', '*', "1 ORDER BY cellorder DESC, cellid DESC");
	func::arrayTwoCahce($authlist, DIR_CACHE.'sys', 'auth');
	func::showMsg('提交成功', '../', 'admin.php?action=roles&operation=cell');
    }
    else
    {
	func::showMsg('没有提交成功，返回采取其他相应的处理', '../', 'admin.php?action=roles&operation=cell');
    }

}

//删除权限
if ($operation == "detelecell")
{
    $id = isset($_GET['id']) ? $_GET['id'] : "";
    //删除文件数据库
    db::DeleteRs(DB_TABLEPRE . 'user_authcells', 'cellid = '.$id);

    //写入缓存
    $authlist = db::getLimit(DB_TABLEPRE . 'user_authcells', '*', "1 ORDER BY cellorder DESC, cellid DESC");
    func::arrayTwoCahce($authlist, DIR_CACHE.'sys', 'auth');
    func::showMsg('删除成功', '../', 'admin.php?action=roles&operation=cell');
}

//读取单个权限
if($cellid && $operation == 'cell')
{
    $cellOne = db::getRow(DB_TABLEPRE . 'user_authcells', '*', " cellid = $cellid ORDER BY cellid DESC");
    !$cellOne && func::showMsg('数据不存在', '../', 'admin.php?action=roles&operation=cell');
}

//权限列表  //或者授予某个用户组的权限时所需权限列表信息
if($operation == 'cell' || $operation == 'auth')
{
    $authlist = db::getLimit(DB_TABLEPRE . 'user_authcells', '*', "1 ORDER BY cellorder DESC, cellid DESC");
}

//下面是处理用户组操作
//user_authgroup
if ($actionPost == 'addgroup')
{
    if ($_POST['groupid'])
    {
	$pid = db::getOne(DB_TABLEPRE . 'user_authgroup', 'groupid', "groupid = '$groupid'");
	if (!$pid)
	{
		 func::showMsg('用户组ID为“'.$groupid.'” 不已经存在！', '../');
	}

	//更新语句
	$sql = 'UPDATE ' . DB_TABLEPRE . 'user_authgroup SET';
	$sql .= " groupname = '$groupname',";
	$sql .= " grouporder = '$grouporder',";
	$sql .= " grouptype = '$grouptype'";
	$sql .= " WHERE groupid = $groupid ";
    }
    else
    {
	(!$groupname || strlen($groupname) > 254) && func::showMsg("长度要求在200字符以下，请重新输入！", '../');
	$pid = db::getOne(DB_TABLEPRE . 'user_authgroup', 'groupid', " groupname = '$groupname' ");
	if ($pid || !$groupname)
	{
		 func::showMsg('“'.$username.'” 已经存在！或者用户组名称不为空', '../');
	}

	//插入语句
	$sql = 'INSERT INTO '.DB_TABLEPRE . 'user_authgroup(';
	$sql .= " groupname,";
	$sql .= " groupcells,";
	$sql .= " grouporder,";
	$sql .= "  grouptype";
	$sql .= ") VALUES(";
	$sql .= " '$groupname',";
	$sql .= " '$groupcells',";
	$sql .= " '$grouporder',";
	$sql .= " '$grouptype'";
	$sql .= ")";
    }
    //插入或者更新资料
    $isok = db::Query($sql);
    if($isok)
    {
	//写入缓存
	$grouplist = db::getLimit(DB_TABLEPRE . 'user_authgroup', $field = '*', '1 ORDER BY grouporder DESC, groupid DESC');
	func::arrayTwoCahce($grouplist, DIR_CACHE.'sys', 'group');
	func::showMsg('提交成功', '../', 'admin.php?action=roles&operation=group');   
    }
}
//删除用户组
if ($operation == "detelegroup")
{
    $id = isset($_GET['id']) ? $_GET['id'] : "";
    //删除文件数据库
    db::DeleteRs(DB_TABLEPRE . 'user_authgroup', 'groupid = '.$id);

    //写入缓存
    $grouplist = db::getLimit(DB_TABLEPRE . 'user_authgroup', $field = '*', '1 ORDER BY grouporder DESC, groupid DESC');
    func::arrayTwoCahce($grouplist, DIR_CACHE.'sys', 'group');
    func::showMsg('成功', '../', 'admin.php?action=roles&operation=group');
}

//用户组列表
if($operation == 'group')
{
    $grouplist = db::getLimit(DB_TABLEPRE . 'user_authgroup', $field = '*', '1 ORDER BY grouporder DESC, groupid DESC');
}

//读取单个用户组 //或者授予某个用户组的权限时所需用户组信息
if($groupid && ($operation == 'group' || $operation == 'auth'))
{
    $groupOne = db::getRow(DB_TABLEPRE . 'user_authgroup', '*', "groupid = $groupid ORDER BY groupid DESC");
    $groupOne['groupcells'] = explode(",", $groupOne['groupcells']);
}

//修改用户组的权限配置
if ($actionPost == 'addauth')
{
    if ($_POST['groupid'])
    {
	$pid = db::getOne(DB_TABLEPRE . 'user_authgroup', 'groupid', "groupid = '$groupid'");
	if (!$pid)
	{
		 func::showMsg('ID为“'. $groupid . '” 不已经存在！', '../');
	}

	$order			= isset($_POST['order']) ? $_POST['order'] : 0;
	$keyid			= isset($_POST['keyid']) ? $_POST['keyid'] : 0;	

	for($i=0; $i<sizeof($order); $i++)
	{
		$ipost = $order[$i];
		$keyIn[] = $keyid[$ipost];
	}
	$keyIn = implode(",", $keyIn);
	//赋予权限
	//更新语句
	$sql = 'UPDATE ' . DB_TABLEPRE . 'user_authgroup SET';
	$sql .= " groupcells = '$keyIn'";
	$sql .= " WHERE groupid = $groupid ";
    }
    //插入或者更新资料
    $isok = db::Query($sql);
    if($isok)
    {
	//写入缓存
	$grouplist = db::getLimit(DB_TABLEPRE . 'user_authgroup', $field = '*', '1 ORDER BY grouporder DESC, groupid DESC');
	func::arrayTwoCahce($grouplist, DIR_CACHE.'sys', 'group');
	func::showMsg('提交成功', '../', 'admin.php?action=roles&operation=auth&groupid=' . $groupid); 
    }
}

if($operation == 'auth' && !$groupid)
{
    func::showMsg('用户组ID参数错误请返回起始页面重新处理', '../');
}

//组件设定
if ($operation == 'app')
{
    //获取所组件配置
    $path = DIR_DATA.'sys';
    $filename = "userapp.php";
    $filetype = "php";
    if (file_exists($path.'/'.$filename))
    {	
	$userapp = func::readFiles($path.'/'.$filename);
	$userapp = unserialize($userapp);
    }
    else
    {
	$userapp = array();  
    }
    
    //删除
    $id = isset($_GET['id']) ? $_GET['id'] : 0;
    if($_GET['actiondel'] == 'appdel' && $id)
    {
	unset($userapp[$id]);
	func::writeFiles($path.'/'.$filename, serialize($userapp));
	func::showmsg("组件设置操作成功！", '../', 'admin.php?action=roles&operation=app');
    }

    $appid = isset($_GET['appid']) ? $_GET['appid'] : 0;
    //缓存提交
    if($actionPost == 'addapp')
    {
	$appid = isset($_POST['appid']) ? $_POST['appid'] : 0;
	!$appid && $appid = time();
	$appname = isset($_POST['appname']) ? $_POST['appname'] : '';
	$appurl = isset($_POST['appurl']) ? $_POST['appurl'] : '';
	$apporder = isset($_POST['apporder']) ? $_POST['apporder'] : 0;

	$userapp[$appid]['appid'] = $appid;
	$userapp[$appid]['userid'] = $userid;
	$userapp[$appid]['appname'] = $appname;
	$userapp[$appid]['appurl'] = $appurl;
	$userapp[$appid]['apporder'] = $apporder;

	func::writeFiles($path.'/'.$filename, serialize($userapp));
	func::showmsg("组件设置操作成功！", '../', 'admin.php?action=roles&operation=app');
    }elseif($appid)
    {
	$variableapp = $userapp[$appid];
    }
}

//将来用到
//复原原始密码passwordisok
if ($operation == 'password')
{
    if ($_GET['userid'])
    {
	$userpass = isset($_POST['userpass']) ? $_POST['userpass'] : "";
	!$userpass && $userpass = isset($_GET['userpass']) ? $_GET['userpass'] : "";
	$userpass = md5($userpass); 
	//更新语句
	$sql = 'UPDATE ' . DB_TABLEPRE . 'user_basic SET';
	$sql .= " userpass = '$userpass'";
	$sql .= " WHERE userid = $userid ";
	db::Query($sql) && func::showMsg('更新成功 , 密码：passwordisok ', '../', 'admin.php?action=roles&operation=password');
    }
}
?>
